package leet.ku._290;

import java.util.HashMap;
import java.util.Map;

public class Solution {
	public boolean wordPattern(String pattern, String str) {
		Map<String, Integer> wordToPattern = new HashMap<String, Integer>();
		String[] patternToWord = new String[26];
		
		String[] words = str.split(" ");
		if (pattern.length() != words.length)
			return false;
		
		for (int i = 0; i < words.length; i++) {
			int p = pattern.charAt(i) - 'a';
			if (patternToWord[p] != null && !patternToWord[p].equals(words[i]))
				return false;
			if (wordToPattern.get(words[i]) != null && !wordToPattern.get(words[i]).equals(p))
				return false;
			patternToWord[p] = words[i];
			wordToPattern.put(words[i], p);
		}
		
		return true;
	}
	
	public static void main(String[] args) {
		Solution s = new Solution();
		System.out.println(s.wordPattern("abba", "dog cat cat dog"));
	}
}
